using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Acme.BookStore.Domain.Entities;
using Acme.BookStore.Gongyings.Dtos;
using Acme.BookStore.Repositories;
using Microsoft.AspNetCore.Mvc;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace Acme.BookStore.Gongyings
{
    public class GongyingAppService : ApplicationService, IGongyingAppService
    {
        private readonly IGongyingRepository _gongyingRepository;

        public GongyingAppService(IGongyingRepository gongyingRepository)
        {
            _gongyingRepository = gongyingRepository;
        }

        public async Task<GongyingDto> GetAsync(string id)
        {
            var gongying = await _gongyingRepository.GetAsync(id);
            var gongyingDto = ObjectMapper.Map<Gongying, GongyingDto>(gongying);
            gongyingDto.SupplierCode = gongying.Id; // 设置供应商编码
            return gongyingDto;
        }

        public async Task<PagedResultDto<GongyingDto>> GetListAsync(PagedAndSortedResultRequestDto input)
        {
            var totalCount = await _gongyingRepository.GetCountAsync(null);

            var gongyings = await _gongyingRepository.GetListAsync(
                input.SkipCount,
                input.MaxResultCount,
                input.Sorting ?? "Id",
                null);

            var gongyingDtos = ObjectMapper.Map<List<Gongying>, List<GongyingDto>>(gongyings);
            
            // 设置供应商编码
            foreach(var dto in gongyingDtos)
            {
                dto.SupplierCode = dto.Id;
            }

            return new PagedResultDto<GongyingDto>(totalCount, gongyingDtos);
        }

        [HttpGet("api/app/gongying/search-by-name")]
        public async Task<PagedResultDto<GongyingDto>> SearchByNameAsync(GongyingNameSearchDto input)
        {
            var totalCount = await _gongyingRepository.GetCountByNameAsync(input.NameKeyword);

            var gongyings = await _gongyingRepository.SearchByNameAsync(
                input.SkipCount,
                input.MaxResultCount,
                input.Sorting ?? "Id",
                input.NameKeyword
            );

            var gongyingDtos = ObjectMapper.Map<List<Gongying>, List<GongyingDto>>(gongyings);
            
            // 设置供应商编码
            foreach(var dto in gongyingDtos)
            {
                dto.SupplierCode = dto.Id;
            }

            return new PagedResultDto<GongyingDto>(totalCount, gongyingDtos);
        }

        public async Task<GongyingDto> CreateAsync(CreateGongyingDto input)
        {
            // 使用GUID作为供应商编码
            var id = Guid.NewGuid().ToString("N");
            
            // 创建实体
            var gongying = new Gongying(id)
            {
                FullName = input.FullName,
                Address = input.Address,
                Phone = input.Phone,
                Remarks = input.Remarks,
                EnterpriseAttribute = input.EnterpriseAttribute,
                MainContact = input.MainContact,
                Mobile = input.Mobile,
                EmailAddress = input.EmailAddress
            };

            await _gongyingRepository.InsertAsync(gongying);

            var gongyingDto = ObjectMapper.Map<Gongying, GongyingDto>(gongying);
            gongyingDto.SupplierCode = gongying.Id; // 设置供应商编码
            
            return gongyingDto;
        }

        public async Task<GongyingDto> UpdateAsync(string id, CreateGongyingDto input)
        {
            var gongying = await _gongyingRepository.GetAsync(id);

            gongying.FullName = input.FullName;
            gongying.Address = input.Address;
            gongying.Phone = input.Phone;
            gongying.Remarks = input.Remarks;
            gongying.EnterpriseAttribute = input.EnterpriseAttribute;
            gongying.MainContact = input.MainContact;
            gongying.Mobile = input.Mobile;
            gongying.EmailAddress = input.EmailAddress;

            await _gongyingRepository.UpdateAsync(gongying);

            var gongyingDto = ObjectMapper.Map<Gongying, GongyingDto>(gongying);
            gongyingDto.SupplierCode = gongying.Id; // 设置供应商编码
            
            return gongyingDto;
        }

        public async Task DeleteAsync(string id)
        {
            await _gongyingRepository.DeleteAsync(id);
        }
    }
} 